package com.ikingtech.platform.service.wechat.mini.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.wechat.mini.model.WechatMiniQueryParamDTO;
import com.ikingtech.platform.service.wechat.mini.entity.WechatMiniDO;
import com.ikingtech.platform.service.wechat.mini.mapper.WechatMiniMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author tie yan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WechatMiniService extends ServiceImpl<WechatMiniMapper, WechatMiniDO> {

    public Boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<WechatMiniDO>lambdaQuery().eq(WechatMiniDO::getId, id));
    }

    public Boolean nameExist(String name) {
        return this.baseMapper.exists(Wrappers.<WechatMiniDO>lambdaQuery().isNotNull(WechatMiniDO::getId).eq(WechatMiniDO::getName, name));
    }

    public Boolean appIdExist(String appId) {
        return this.baseMapper.exists(Wrappers.<WechatMiniDO>lambdaQuery().isNotNull(WechatMiniDO::getId).eq(WechatMiniDO::getAppId, appId));
    }

    public Boolean nameExist(String id, String name) {
        return this.baseMapper.exists(Wrappers.<WechatMiniDO>lambdaQuery().ne(WechatMiniDO::getId, id).eq(WechatMiniDO::getName, name));
    }

    public Boolean appIdExist(String id, String appId) {
        return this.baseMapper.exists(Wrappers.<WechatMiniDO>lambdaQuery().ne(WechatMiniDO::getId, id).eq(WechatMiniDO::getAppId, appId));
    }

    public WechatMiniDO getByWechatMiniId(String wechatMiniId) {
        return this.getOne(Wrappers.<WechatMiniDO>lambdaQuery().eq(WechatMiniDO::getCode, wechatMiniId));
    }

    public PageResult<WechatMiniDO> listPage(WechatMiniQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<WechatMiniDO>lambdaQuery()
                .eq(Tools.Str.isNotBlank(queryParam.getWechatMiniId()), WechatMiniDO::getCode, queryParam.getWechatMiniId())
                .like(Tools.Str.isNotBlank(queryParam.getName()), WechatMiniDO::getName, queryParam.getName())
                .eq(Tools.Str.isNotBlank(queryParam.getAppId()), WechatMiniDO::getAppId, queryParam.getAppId())));
    }
}
